package model;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


public class Bear extends Animal{

// Characteristics shared by all Bears (class variables).
    
    // The age at which a bear can start to breed.
    private static int BREEDING_AGE = 15;
    // The age to which a bear can live.
    private static int MAX_AGE = 200;
    // The likelihood of a bear breeding.
    private static double BREEDING_PROBABILITY = 0.04;
    // The maximum number of births.
    private static int MAX_LITTER_SIZE = 2;
    // The food value of a single fox. In effect, this is the
    // number of steps a wolf can go before it has to eat again.
    private static final int FOX_FOOD_VALUE = 10;
    // The food value of a single rabbit. In effect, this is the
    // number of steps a wolf can go before it has to eat again.
    private static final int RABBIT_FOOD_VALUE = 6;
    // A shared random number generator to control breeding.
    private static final Random rand = Randomizer.getRandom();
    
    // Individual characteristics (instance fields).


    public Bear(){}
    
    /**
     * Create a bear. A bear can be created as a new born (age zero
     * and not hungry) or with a random age and food level.
     * 
     * @param randomAge If true, the bear will have random age and hunger level.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Bear(boolean randomAge, Field field, Location location)
    {
        super(field, location);
        if(randomAge) {
            setAge(rand.nextInt(MAX_AGE));
            foodLevel = rand.nextInt(FOX_FOOD_VALUE);
        }
        else {
            setAge(0);
            foodLevel = FOX_FOOD_VALUE;
        }
    }
    
    /**
     * This is what the bear does most of the time: it hunts for
     * rabbits. In the process, it might breed, die of hunger,
     * or die of old age.
     * @param field The field currently occupied.
     * @param newFoxes A list to return newly born foxes.
     */
    public void act(List<Actor> newAnimals)
    {
    	super.act(newAnimals);
        if(isAlive()) {
            giveBirth(newAnimals);            
        }
    }
    
    /**
     * Look for rabbits adjacent to the current location.
     * Only the first live rabbit is eaten.
     * @return Where food was found, or null if it wasn't.
     */
    protected Location findFood()
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object animal = field.getObjectAt(where);
            if(animal instanceof Fox) {
            	Fox Fox = (Fox) animal;
                if(Fox.isAlive() && (foodLevel < FOX_FOOD_VALUE || foodLevel + FOX_FOOD_VALUE < 12)) { 
                	Fox.setDead();
                    foodLevel += FOX_FOOD_VALUE;
                    return where;
                }
            }
            if(animal instanceof Rabbit) {
                Rabbit rabbit = (Rabbit) animal;
                if(rabbit.isAlive() && (foodLevel < RABBIT_FOOD_VALUE || foodLevel + RABBIT_FOOD_VALUE < 12)) { 
                	rabbit.setDead();
                    foodLevel += RABBIT_FOOD_VALUE;
                    return where;
                }
            }
        }
        return null;
    }

	@Override
	public boolean isActive() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public int getMaxAge() {
		return MAX_AGE;
	}

	@Override
	public int getBreedingAge() {
		return BREEDING_AGE;
	}

	@Override
	public double getBreedingProbability() {
		return BREEDING_PROBABILITY;
	}

	@Override
	public int getMaxLitterSize() {
		return MAX_LITTER_SIZE;
	}
	
	@Override
	Animal newYoung(Location location) {
		return new Bear(false, getField(), location);
	}

	@Override
	public void setMaxAge(int maxAge) {
		MAX_AGE = maxAge;
	}

	@Override
	public void setBreedingAge(int breedingAge) {
		BREEDING_AGE = breedingAge;
	}

	@Override
	public void setBreedingProbability(double breedingProbability) {
		BREEDING_PROBABILITY = breedingProbability;
	}

	@Override
	public void setMaxLitterSize(int maxLitterSize) {
		MAX_LITTER_SIZE = maxLitterSize;
	}
	
	public String toString() {
		return "Bear";
	}

}